﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using MiniExcelLibs;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using Zo.ExcelBox.Base;
using Zo.ExcelBox.Models;
using Zo.ExcelBox.Pages;

namespace Zo.ExcelBox.ViewModels
{
    public partial class JudicialCaseVM : ObservableObject
    {

        private ObservableCollection<JudicialCase> _tableDatas;
        public ObservableCollection<JudicialCase> TableDatas
        {
            get => _tableDatas;
            set => SetProperty(ref _tableDatas, value);
        }

        private string _name = string.Empty;
        /// <summary>
        /// 模版名称
        /// </summary>
        public string Name
        {
            get => _name;
            set => SetProperty(ref _name, value);
        }

        private int _pageIndex = 1;
        /// <summary>
        /// 模版名称
        /// </summary>
        public int PageIndex
        {
            get => _pageIndex;
            set => SetProperty(ref _pageIndex, value);
        }

        private int _pageSize = 20;
        /// <summary>
        /// 每页的数据量
        /// </summary>
        public int PageSize
        {
            get => _pageSize;
            set => SetProperty(ref _pageSize, value);
        }

        private int _maxPageCount = 0;
        /// <summary>
        /// 最大页数
        /// </summary>
        public int MaxPageCount
        {
            get => _maxPageCount;
            set => SetProperty(ref _maxPageCount, value);
        }

        /// <summary>
        /// 添加页面
        /// </summary>
        public IRelayCommand OnAddCommand { get; }

        /// <summary>
        /// 修改页面
        /// </summary>
        public IRelayCommand<JudicialCase> OnUpdateCommand { get; }

        /// <summary>
        /// 删除页面
        /// </summary>
        public IRelayCommand<JudicialCase> OnDeleteCommand { get; }

        /// <summary>
        /// 分页改变
        /// </summary>
        public IRelayCommand<int> OnPageChangeCommand { get; }


        /// <summary>
        /// 搜索
        /// </summary>
        public IRelayCommand OnSearchCommand { get; }


        /// <summary>
        /// 数据访问
        /// </summary>
        public static SqlSugarClient _db;

        /// <summary>
        /// 案件管理
        /// </summary>
        public JudicialCaseVM()
        {
            OnAddCommand = new RelayCommand(OnAdd);
            OnUpdateCommand = new RelayCommand<JudicialCase>(OnUpdate);
            OnPageChangeCommand = new RelayCommand<int>(OnPageChange);
            OnSearchCommand = new RelayCommand(OnSearch);
            OnDeleteCommand = new RelayCommand<JudicialCase>(OnDelete);

            _db = new DbContext().DataBase;

            LoadData();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        void LoadData(int page = 1)
        {

            var total = 0;
            var datas = _db.Queryable<JudicialCase>()
                .WhereIF(!string.IsNullOrWhiteSpace(Name), w => w.Name.Contains(Name))
                .ToPageList(page, PageSize, ref total);

            TableDatas = new ObservableCollection<JudicialCase>(datas);
            MaxPageCount = CommonHelper.CalculateTotalPages(total, PageSize);
        }


        void OnPageChange(int page)
        {
            LoadData(page);
        }

        void OnSearch()
        {
            LoadData();
        }


        void OnAdd()
        {
            OnEditPage();
        }

        void OnUpdate(JudicialCase map)
        {
            OnEditPage(map.Id);
        }

        void OnDelete(JudicialCase map)
        {

            var result = HandyControl.Controls.MessageBox.Show("确认要删除吗？", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
            if (result == MessageBoxResult.OK)
            {
                if (_db.Deleteable<JudicialCase>().Where(w => w.Id == map.Id).ExecuteCommand() > 0)
                {
                    Growl.Success("删除成功");
                    LoadData();
                }
                else
                {
                    Growl.Error("删除失败");
                }
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        private void OnEditPage(int id = 0)
        {
            var win = new JudicialCasePageEditWin(id);
            win.OnClose += (type) =>
            {
                if (type == WindowCloseType.Succee)
                {
                    LoadData();
                }
            };
            //win.Height = 300;
            //win.Width = 500;
            win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            win.Owner = System.Windows.Application.Current.MainWindow;
            win.SizeToContent = SizeToContent.Manual;

            win.ShowDialog();
        }

    }


    /// <summary>
    /// 编辑页面
    /// </summary>
    public partial class JudicialCaseEditVM : ObservableObject
    {

        /// <summary>
        /// 表单数据
        /// </summary>
        public JudicialCase FormData { get; set; } = new JudicialCase();


        /// <summary>
        /// 提交数据
        /// </summary>
        public IRelayCommand OnSubmitCommand { get; }

        /// <summary>
        /// 取消
        /// </summary>
        public IRelayCommand OnCancelCommand { get; }


        /// <summary>
        /// 关闭窗口
        /// </summary>
        public event Action<WindowCloseType> OnClose;

        /// <summary>
        /// 数据访问
        /// </summary>
        public static SqlSugarClient _db;


        /// <summary>
        /// 页面ID
        /// </summary>
        public int PageId { get; private set; }


        /// <summary>
        /// 
        /// </summary>
        public JudicialCaseEditVM(int id)
        {

            PageId = id;
            OnSubmitCommand = new RelayCommand(OnSubmit);
            OnCancelCommand = new RelayCommand(OnCancel);

            _db = new DbContext().DataBase;

            Get(id);
        }

        private void Get(int id)
        {
            if (id > 0)
            {
                FormData = _db.Queryable<JudicialCase>().Where(w => w.Id == id).First();
            }

        }



        /// <summary>
        /// 提交
        /// </summary>
        void OnSubmit()
        {

            var row = 0;
            if (PageId > 0)
            {
                row = _db.Updateable(FormData).ExecuteCommand();
            }
            else
            {
                row = _db.Insertable(FormData).ExecuteCommand();
            }


            if (row > 0)
            {
                Growl.Success("保存成功");
                OnClose?.Invoke(WindowCloseType.Succee);
            }
            else
            {
                HandyControl.Controls.MessageBox.Show("保存失败");
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        void OnCancel()
        {
            OnClose?.Invoke(WindowCloseType.Cancel);
        }
    }
}
